import { getCanBuyDays, getCalendartickt } from './src/thirdServices/palaceApi';
import PalaceHelper from './src/middleware/palaceHelper';

const getPalaceTicketPool = async (appointDate, appointTime) => {
    let ticketPool;
    const res = await getCanBuyDays();
    if (res.data.code === 200) {
      const canJson = JSON.parse(res.data.data);
      const date = new Date(canJson.time);
      const dateList: string[] = [];
      for (let i = 0;i< canJson.days; i++) {
          let day = date.getDate();
          date.setDate(day + 1);
          const year = date.getFullYear();
          const month = date.getMonth() + 1;
          const monthStr = month < 10 ? `0${month}` : `${month}`
          const d = date.getDate();
          const dStr = d < 10  ? `0${d}` : `${d}`;
          dateList.push(`${year}-${monthStr}-${dStr}`);
      }
      
      if (dateList.includes(appointDate)) {
        const date = new Date(appointDate);
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const monthStr = month < 10 ? `0${month}` : `${month}`
        const params = {
          parkId: 11324,
          year,
          month: monthStr,
          merchantId: 2655,
          merchantInfoId: 2655
        };
  
        const calendarRes = await getCalendartickt(params);
  
        if (calendarRes.data.status === 200) {
          const calendarList = calendarRes.data.data;
          const vo = calendarList.find(item => item.occDate === appointDate);
          if (vo.saleStatus === 'T') {
            if (vo.parkFsyyDetailDTOS) {
              if (appointTime != undefined) {
                // 指定时间段
                const dto = vo.parkFsyyDetailDTOS[appointTime - 1];
                if (dto.stockNum) {
                  ticketPool = vo;
                  ticketPool.DTO = dto;
                }
              } else {
                // 不指定时间段
                const dto = vo.parkFsyyDetailDTOS.find(dto => !!dto.stockNum);
                if (dto) {
                  ticketPool = vo;
                  ticketPool.DTO = dto;
                }
              }
            }
          } else {
            console.log('闭馆');
          }
        }
      }
    }
    return ticketPool;
  }

const createPalaceTicket = async (poolVO, touristList) => {
  const palaceHelper = new PalaceHelper();
  const isBuy = await palaceHelper.buy(poolVO, touristList);
  return isBuy;
};

const buyPalaceTicket = async (appointDate, touristList) => {
  const ticketPool = await getPalaceTicketPool(appointDate, 1);
  let isBuy = false;
  if (ticketPool) {
    try {
        console.log('有票')
      isBuy = await createPalaceTicket(ticketPool, touristList);
    } catch (error) {
      
    }
  } else {
    console.log('没票');
  }
  return isBuy;
}


const buy = async () => {
    const isBuy = await buyPalaceTicket('2024-04-14', [{
      ticketType: '标准票',
      name: '朱文娅',
      idType: '0',
      idNo: '320981198510080468'
    }]);

    if (!isBuy) {
        setTimeout(() => {
            buy();
        }, 5000);
    }
};

const main = () => {
    buy();
}

main()